package dryven.util.crypt;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AESEncryption {
	private AESGeneratedKey _key;
	private String _message;
	
	public AESEncryption(AESGeneratedKey key, String originalText) {
		super();
		_key = key;
		_message = originalText;
	}
	
	public String encrypt() {
		try {
			SecretKeySpec skeySpec = new SecretKeySpec(_key.getKey(), "AES");
		    Cipher cipher = Cipher.getInstance("AES");
		    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		    byte[] encrypted = cipher.doFinal(_message.getBytes());
		    return Base64.encodeToString(encrypted, false);
		} catch(Exception e) {
			throw new RuntimeException("Unable to encrypt", e);
		}
	}
}
